//获取验证码
var pwdReg= /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/;
var $eyeOpen = $(".js-eye-open");

//-----------------找回
var $oldPassword1 = $("#oldPassword1");
var $oldPassword2 = $("#oldPassword2");
var $newPassword1 = $("#newPassword1");
var $newPassword2 = $("#newPassword2");

var flagoldPassword1 = false;//公共flag 判断是不是验证通过
var flagoldPassword2 = false;//公共flag 判断是不是验证通过
var flagnewPassword1 = false;//公共flag 判断是不是验证通过
var flagnewPassword2 = false;//公共flag 判断是不是验证通过

(function(){
		
	 $eyeOpen.on("click", lookPwd);
        $("#submitBtn").on("click", submit);
	
	//原密码输入框事件
    $oldPassword1.on("onreadystatechange input blur", function(){
      var $this = $(this);
      var $cleartext = $this.next("input[type='text']");
      var oldPassword = $this.val();

      $cleartext.val(oldPassword);

      flagoldPassword1 = validateForm($this, oldPassword, pwdReg, "请输入正确的原密码", $cleartext);
    });

    //明文原密码输入框事件
    $oldPassword2.on("onreadystatechange input blur", function(){
        var $this = $(this);
        var $cleartext = $this.prev("input[type='password']");
        var oldPassword = $this.val();

        $cleartext.val(oldPassword);

        flagoldPassword2 = validateForm($this, oldPassword, pwdReg, "请输入正确的原密码", $cleartext);
    });
	
	//新密码输入框事件
    $newPassword1.on("onreadystatechange input blur", function(){
      var $this = $(this);
      var $cleartext = $this.next("input[type='text']");
      var newPassword = $this.val();

      $cleartext.val(newPassword);

      flagnewPassword1 = validateForm($this, newPassword, pwdReg, "请输入正确的新密码", $cleartext);
    });

    //明文新密码输入框事件
    $newPassword2.on("onreadystatechange input blur", function(){
        var $this = $(this);
        var $cleartext = $this.prev("input[type='password']");
        var newPassword = $this.val();

        $cleartext.val(newPassword);

        flagnewPassword2 = validateForm($this, newPassword, pwdReg, "请输入正确的新密码", $cleartext);
    });

        //表单验证, 前面四个都是必传参数：当前输入框、当前值、正则、提示文字，最后一个参数是可选参数：查看明文密码
        function validateForm(that, value, reg, txt, cleartext){
          var $parent = that.closest(".form-group");
          var $addon = $parent.find(".input-group-addon");
          var $msg = $parent.find(".js-line-alert");
          var $msgText = $parent.find(".js-alert-text");
          
          var flag = true;

          if(value.length === 0){
            modifyWarning($msgText, txt);
            showWarning($addon, that, $msg, cleartext);
            flag = false;
          }
          else if(!reg.test(value)){
            modifyWarning($msgText, txt);
            showWarning($addon, that, $msg, cleartext);
            flag = false;
          }
          else{
            hideWarning($addon, that, $msg, cleartext);
          }

          return flag;
        }

        //输入框边框变红并显示提示文字
        function showWarning(addon, input, msg, cleartext){
            if(cleartext){
              cleartext.addClass("input-error border-error");
            }
            addon.addClass("border-error");
            input.addClass("input-error border-error");
            msg.show();
        }

    	//输入框边框恢复原色并隐藏提示文字
        function hideWarning(addon, input, msg, cleartext){
            if(cleartext){
              cleartext.removeClass("input-error border-error");
            }
            addon.removeClass("border-error");
            input.removeClass("input-error border-error");
            msg.hide();
        }

    	//修改提示文字信息
        function modifyWarning(element, txt){
            element.text(txt);
        }

    	//查看明文密码
        function lookPwd(){
          var $pwd = $(this).closest(".input-group").find("input[type='password']");
          var $pwd_show = $pwd.next("input[type='text']");
          var pwd = $pwd.val();
          var pwd_show = $pwd_show.val();
        
          if($pwd.is(":visible")){
              $pwd.hide();
              $pwd_show.val(pwd).show();
          }
          else{
              $pwd.val(pwd_show).show();
              $pwd_show.hide();
          }
        }
	
		//注册
    function submit(){
      var oldPassword = $('#oldPassword1').val();
			var newPassword = $('#newPassword1').val();

      $oldPassword1.blur();
      $oldPassword2.blur();
      $newPassword1.blur();
      $newPassword2.blur();
			
			if(flagoldPassword1 && flagoldPassword2 && flagnewPassword1 && flagnewPassword2){
				$.ajax({
        			url: "upPwd",
        			type: "post",
        			dataType: 'json',
        			data : {
					memberId : $('#memberId').val(),
        				oldPwd : oldPassword,
						newPwd :newPassword
        			},
        			success: function(data) {
        				if (data.returnFlag == '0') {
                            layer.open({
                                content: '修改密码成功！',
                                scrollbar: false
                            });
        					location.href = "../user/myPc";
						      //location.reload();
        				}else {
							if (data.returnCode == 'A002') {
                                layer.open({
                                    content: '您的帐号不存在!',
                                    scrollbar: false
                                });
        					}else{
                                layer.open({
                                    content: '您的原密码错误!',
                                    scrollbar: false
                                });
							}
        				}
        			},
        			error: function(xhr, type, errorThrown) {
        				//异常处理；
						alert("请稍后再试!");
        			}
        		});
			
			}
    }
		
})();